home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / SAYTIME.ICN < prev    next >
Text File  |  1992-11-20  |  3KB  |  78 lines

  1. ############################################################################
  2. #
  3. #    File:     saytime.icn
  4. #
  5. #    Subject:  Procedure to produce the time in English
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     May 20, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  saytime() -- Computes the time in natural English.  If an argument is
  14. #  supplied it is used as a test value to check the operation of the
  15. #  program.
  16. #
  17. ############################################################################
  18.  
  19. procedure saytime(time)
  20.    local hour,min,mod,near,numbers,out,sec
  21.    #
  22.    # Extract the hours, minutes, and seconds from the time.
  23.    #
  24.    /time := &clock
  25.    time ? {
  26.       hour := integer(tab(find(":") | 0)) | fail
  27.       move(1)
  28.       min := tab(find(":") | 0)
  29.       move(1)
  30.       sec := tab(0)
  31.       }
  32.    min := integer(min) | 0
  33.    sec := integer(sec) | 0
  34.    #
  35.    # Now start the processing in earnest.
  36.    #
  37.    near := ["just gone","just after","nearly","almost"]
  38.    if sec > 29 then min +:= 1    # round up minutes
  39.    mod := min % 5                # where we are in 5 minute bracket
  40.    out := near[mod] || " " | ""  # start building the result
  41.    if min > 32 then hour +:= 1   # we are TO the hour
  42.    min +:= 2             # shift minutes to straddle the 5-minute point
  43.    #
  44.    # Now special-case the result for Noon and Midnight hours.
  45.    #
  46.    if hour % 12 = 0 & min % 60 <= 4 then {
  47.       return if hour = 12 then out || "noon"
  48.                     else out || "midnight"
  49.       }
  50.    min -:= min % 5               # find the nearest 5 mins
  51.    if hour > 12 then hour -:= 12 # get rid of 25-hour clock
  52.    else if hour = 0 then hour := 12 # .. and allow for midnight
  53.    #
  54.    # Determine the phrase to use for each 5-minute segment.
  55.    #
  56.    case min of {
  57.        0: {}                      # add "o'clock" later
  58.       60: min=0                   # ditto
  59.        5: out ||:= "five past"
  60.       10: out ||:= "ten past"
  61.       15: out ||:= "a quarter past"
  62.       20: out ||:= "twenty past"
  63.       25: out ||:= "twenty-five past"
  64.       30: out ||:= "half past"
  65.       35: out ||:= "twenty five to"
  66.       40: out ||:= "twenty to"
  67.       45: out ||:= "a quarter to"
  68.       50: out ||:= "ten to"
  69.       55: out ||:= "five to"
  70.       }
  71.    numbers := ["one","two","three","four","five","six",
  72.              "seven","eight","nine","ten","eleven","twelve"]
  73.    out ||:= (if *out = 0 then "" else " ") || numbers[hour]
  74.                  # add the hour number
  75.    if min = 0 then out ||:= " o'clock" # .. and o'clock if exact
  76.    return out                    # return the final result
  77. end
  78.